package com.imooc.rabbitmq.handack;

import com.imooc.rabbitmq.utils.ConnectionUtils;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;

public class Producer {
    private static final String exchangeName = "test_ack_exchange";
    private static final String routingKey = "ack.save";

    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = ConnectionUtils.getConnection();

        //2. 通过Connection 创建一个channel
        Channel channel = connection.createChannel();


        for (int i = 0; i < 5; i++) {
            String msg = "Hello RabbitMQ! ACK Message i="+i;
            Map<String,Object> headers = new HashMap<String,Object>();
            headers.put("num",i);
            AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder()
                    .deliveryMode(2)
                    .contentEncoding("UTF-8")
                    .headers(headers)
                    .build();
            channel.basicPublish(exchangeName,routingKey,properties,msg.getBytes());
        }

        channel.close();
        connection.close();
    }
}
